package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Entity.User;

public class userDao extends BaseDao {
	public static User getUser(String userName) throws SQLException
	{
			ResultSet result;
			result = BaseDao.executeQuery("select * from Users where UserName="+userName);
			if(result.next())
			{
				return new User(result.getString(1),result.getString(2),result.getString(3));
			}
			else
			{
				return null;
			}
	}
	public static void insertUser(String userName,String password,String userType) throws SQLException
	{
		PreparedStatement ptst= BaseDao.prepareStatement("insert into Users values(?,?,?)");
		ptst.setString(1, userName);
		ptst.setString(2, password);
		ptst.setString(3, userType);
		ptst.execute();
	}
	public static void insertUser(User user) throws SQLException
	{
		insertUser(user.userName,user.password,user.userType);
	}
	public static void updateUser(String userName,String password,String userType) throws SQLException
	{
		PreparedStatement ptst= prepareStatement("update Users set password=?,userType=? where UserName=?");
		ptst.setString(1, password);
		ptst.setString(2, userType);
		ptst.setString(3, userName);
		ptst.execute();
	}
	public static void updateUser(User user) throws SQLException
	{
		updateUser(user.userName,user.password,user.userType);
	}
	public static void deleteUser(User user) throws SQLException
	{
		deleteUser(user.userName);
	}
	public static void deleteUser(String userName) throws SQLException
	{
		PreparedStatement ptst= prepareStatement("delete from Users UserName=?");
		ptst.setString(1, userName);
		ptst.execute();
	}


}
